import random

class Character:
    def __init__(self, name, health, attack):
        # 初始化角色属性
        self.name = name
        self.health = health
        self.attack = attack

    def is_alive(self):
        # 检查角色是否还活着
        return self.health > 0

    def take_damage(self, damage):
        # 受到伤害并更新生命值
        self.health -= damage
        if self.health < 0:
            self.health = 0

    def attack_opponent(self, opponent):
        # 攻击对手并造成随机伤害
        damage = random.randint(0, self.attack)
        opponent.take_damage(damage)
        print(f"{self.name}攻击了{opponent.name},造成了{damage}点伤害!")

class Human(Character):
    def __init__(self, name):
        # 初始化人类角色,继承自Character类
        super().__init__(name, health=100, attack=10)

class Dog(Character):
    def __init__(self, name):
        # 初始化狗角色,继承自Character类
        super().__init__(name, health=50, attack=5)

def game():
    # 创建人类和狗的实例
    human = Human("勇士")
    dog = Dog("凶狗")

    print("人狗大战开始!")

    # 游戏主循环
    while human.is_alive() and dog.is_alive():
        # 人类先攻击
        human.attack_opponent(dog)
        # 如果狗还活着,则反击
        if dog.is_alive():
            dog.attack_opponent(human)
        
        # 显示双方剩余生命值
        print(f"{human.name}剩余生命值: {human.health}")
        print(f"{dog.name}剩余生命值: {dog.health}")
        print("--------------------")

    # 判断胜利者
    if human.is_alive():
        print(f"{human.name}获胜!")
    else:
        print(f"{dog.name}获胜!")

if __name__ == "__main__":
    # 运行游戏
    game()